/*
 * Created by JFormDesigner on Mon Jun 07 20:41:02 CST 2021
 */

package com.J1ay.carsys.gui.User.Dialog;


import java.awt.event.*;
import com.J1ay.carsys.gui.tishi.Success;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.BowCar;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.Impl.UserServiceImpl;
import com.J1ay.carsys.service.UserService;
import com.J1ay.carsys.utils.Common;

import java.awt.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class HuanCarDialog extends JDialog {
    BowCar record;
    Float total,permNum =(float) 1.0;
    UserService userService = new UserServiceImpl();
    public HuanCarDialog(Window owner, BowCar bowCar, Bower bower) {
        super(owner);
        initComponents();
        record = bowCar;
        if(bower.getBperm() == "vip") {
            permNum = (float) 0.9;
            this.label1.setText("尊敬的高级会员,"+bower.getBname()+"您好！");
        }
        else if (bower.getBperm() == "svip") {
            permNum = (float) 0.85;
            this.label1.setText("尊敬的超级会员,"+bower.getBname()+"您好！");
        }
        else{
            permNum = (float) 1.0;
            this.label1.setText("尊敬的普通会员,"+bower.getBname()+"您好！");
        }
        this.getPrice(bowCar);

    }
    public void getPrice(BowCar bowCar){
        int diffday;
        diffday = Common.getDayDiffer(record.getBowCreate(),new Date());
//        不足一天按一天计算
        if(diffday == 0){
            diffday = 1;
        }
        if(diffday<=record.getBowDay()){
            total = bowCar.getBowPrice() * diffday * permNum;
            this.label2.setText("已借天数"+diffday+"天，"+"总价为："+total+"元");
            record.setBowResult("yes");
        }
        else {
            int diff2 = diffday - record.getBowDay();
            total =  bowCar.getBowPrice() * record.getBowDay() * permNum + diff2*(bowCar.getBowPrice() / 10) * permNum;
            this.label2.setText("您已超出"+ diff2 +"天!"+"总价为："+total+"元");
            this.label3.setText("（超出天数将上调单价10%）");
            //            延期还车
            record.setBowResult("fail");
        }
        record.setBowPrice(total);
        record.setBowDay(diffday);
        //            可执行还车操作
    }
    public void EndHuan(){
        record.setBowEnd(new Date());
        record.setBowStatus("done");
        Result result = userService.updateRecord(record);
        System.out.println(record);
        if (200 == result.getStatus()){
            new Success(this,"还车成功!").start();
            this.dispose();
        }
        else{
            new Warning(this,result.getMsg()).start();
        }
    }
    public void start() {
        this.setVisible(true);
    }

    private void huanActionPerformed(ActionEvent e) {
        // TODO add your code here
        this.EndHuan();
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        huan = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();

        //======== this ========
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7ba1\u7406\u7cfb\u7edf");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- huan ----
        huan.setText("\u786e\u5b9a");
        huan.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                huanActionPerformed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(162, 162, 162)
                            .addComponent(huan))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(107, 107, 107)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label3, GroupLayout.PREFERRED_SIZE, 276, GroupLayout.PREFERRED_SIZE)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                    .addComponent(label2, GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                                    .addComponent(label1, GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)))))
                    .addContainerGap(15, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(18, 18, 18)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(label2, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(label3, GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(huan)
                    .addGap(55, 55, 55))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton huan;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
